var url = require('url');
var swig = require('swig');						//模板引擎

var config = require('./config.json');			//项目配置文件

/**
 * 加载路由控制
 */
var routes = require('./routes');

//应用程序实例
var APP = function(){
	return {
		startServer : function(req,res){	//启动服务
			this.req = req;					//request 对象
			this.res = res;					//response 对象

			this.route();					//调用路由控制
		},
		route : function(){					//路由控制
			var pathname = url.parse(this.req.url).pathname;
    		console.log("请求路径: " + pathname);

    		if(typeof(routes.handle[pathname]) === 'function'){
    			routes.handle[pathname](this);				//根据请求调用相应控制
    			return;
    		}

    		//输出404
    		routes.handle['404'](this);
		},
		resHtml : function(html){
			this.res.writeHead(200, {"Content-Type": "text/html"});
      		this.res.write(html);
      		this.res.end();
		},

		config : config,
		swig : swig
	}
};

var app = new APP();

//目录配置
app.app_path = __dirname;
app.view_path = app.app_path+'/views';
app.public_path = app.app_path+'/public';
app.upload_path = app.app_path+'/uploads';


module.exports = app;